Ext.define('WGenius.util.Proxy', {

	singleton: true,

	requires: ['Ext.data.proxy.JsonP'],

	process: function(url, callback) {
		
		var sessionStore = Ext.getStore('Sessions'),
		    ContacterStore = Ext.getStore('Contacters'),
		    sessionContacterStore = Ext.getStore('SessionContacters'),
		    ContacterSessionStore = Ext.getStore('ContacterSessions'),
		    sessionIds, proposalModel, ContacterModel, ContacterSessions = {}, sessionId, Contacter, sessionDays = {};

		Ext.Ajax.request({
		    url: url,
		    //method:'POST',
		    //headers:{'Content-Type':'application/json;utf-8'},
		    //callbackName: 'exchangeData',

		    callback: function(options ,success ,response) {
		    	var data = Ext.decode(response.responseText);
		        Ext.Array.each(data.exchangeData, function(proposal) {
		        	//alert(proposal);
		            proposal.ContacterIds = [];
		            proposalModel = Ext.create('WGenius.model.Session', proposal);

		            Ext.Array.each(proposal.Contacters, function(Contacter) {
		                proposal.ContacterIds.push(Contacter.id);

		                ContacterModel = Ext.create('WGenius.model.Contacter', Contacter);
		                ContacterStore.add(ContacterModel);
		                sessionContacterStore.add(ContacterModel);

		                ContacterSessions[Contacter.id] = ContacterSessions[Contacter.id] || [];
		                ContacterSessions[Contacter.id].push(proposal.id);
		            });

		            if (proposal.date) {
		                sessionDays[proposal.date] = {
		                    day: proposalModel.get('time').getDate(),
		                    text: Ext.Date.format(proposalModel.get('time'), 'm/d'),
		                    time: proposalModel.get('time')
		                };
		            }

		            sessionStore.add(proposalModel);
		            ContacterSessionStore.add(proposalModel);
		        });

		        for (var ContacterId in ContacterSessions) {
		            Contacter = ContacterStore.findRecord('id', ContacterId);
		            if (Contacter) {
		                Contacter.set('sessionIds', ContacterSessions[ContacterId]);
		            }
		        }

		        WGenius.sessionDays = Ext.Array.sort(Ext.Object.getValues(sessionDays), function(a, b) {
		            return a.time < b.time ? -1 : 1;
		        });

		        callback();
		    }
		});

	}
});
